#include "Box.h"
#include <algorithm>

Box::Box(float x, float y, float mass, float width, float height)
{
    this->mass = mass;
    this->width = width;
    this->height = height;
    this->position = vec2(x, y);
    lt = vec2(-width / 2, height / 2);
    rt = vec2(width / 2, height / 2);
    lb = vec2(-width / 2, -height / 2);
    rb = vec2(width / 2, -height / 2);
    edges[0]= vec2(0, 1);
    edges[1] = vec2(1, 0);
    edges[2] = vec2(0, -1);
    edges[3] = vec2(-1, 0);
    this->type = BOX;
}

void Box::update(float dt,SDL_Renderer*renderer)
{
    
    for (auto& f : forces)
    {
        this->velocity += f.getForce();
        if (f.getType() == 0)
        {
            forces.erase(std::find(forces.begin(), forces.end(), f));
        }
    }

    this->position += this->velocity*dt;
    this->axis = glm::axis(rotation);
    glm::mat4 rot = glm::rotate(glm::mat4(1.0f), glm::atan(axis.y,axis.x), glm::vec3(0, 0, 1));

    
}

Rect Box::getRect()
{
    auto y = std::vector<float>{ lt.y, lb.y, rb.y, rt.y };
    std::sort(y.begin(), y.end(), [=](float a, float b) {
return a > b;
        });
    auto top= y[0];
    auto bottom = y[3];
    auto x = std::vector<float>{ lt.x, lb.x, rb.x, rt.x };
    std::sort(x.begin(), x.end(), [=](float a, float b) {
return a > b;
        });
    auto left = x[0];
    auto right = x[3];

    return Rect(vec2(left,bottom),vec2(right,top));
}

